//
//  LyricManager.m
//  作业
//
//  Created by qianfeng on 15/12/8.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "LyricManager.h"
#import "Lyrics.h"

@implementation LyricManager
{
    NSMutableArray * _mArray;
}

//构造方法
-(id)initWithPath:(NSString *)path
{
    if (self = [super init]) {
        
        _mArray = [[NSMutableArray alloc] init];
        [self sortedByTime];
    }
    return self;
}

#pragma mark - 解析歌词
+(void)readAndParseFile:(NSString *)path andLyric:(ParseBlock)block
{
    NSMutableArray * _mArray = [NSMutableArray array];
    NSString * content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray * Line = [content componentsSeparatedByString:@"\n"];
    NSMutableArray * aLine = [NSMutableArray arrayWithArray:Line];
    [aLine removeObject:@""];
    
    for (NSString * obj in aLine) {
        NSMutableArray * time = [[NSMutableArray alloc] init];
        NSString * lrc;
        NSUInteger i = 0,flag = 0;
        //读取时间
        while (i < [obj length] && isnumber([obj characterAtIndex:i+1])){
            NSRange range = {i,10};
            NSString * str = [obj substringWithRange:range];
            [time addObject:str];
            i+=10;
            flag = 1;
        }
        if (flag == 0) {
            continue;
        }
        
        //读取歌词
        if (i < [obj length] &&![obj characterAtIndex:i]) {
            lrc = nil;
        }
        else{
            NSRange range = {i,[obj length]- i};
            lrc = [obj substringWithRange:range];
        }
        //创建对象 存入数组
        for (NSUInteger count = 0; count < [time count] ; count++) {
            Lyrics *l = [[Lyrics alloc] initWithTime:[time objectAtIndex:count] andLyric:lrc];
            [_mArray addObject:l];
            
        }
    }
    
    [_mArray sortUsingSelector:@selector(compareByTime:)];
    
    block(_mArray);
}
#pragma mark - 打印
-(void)print
{
    for (id obj in _mArray) {
        NSLog(@"%@",obj);
    }
}
#pragma mark - 按时间排序
- (void)sortedByTime
{
    
}

#pragma mark - 增加歌词
-(void)addLrcWithTime:(NSString *)time andLrc:(NSString *)lrc
{
    if (![self searchLrc:time]) {
        Lyrics *l = [[Lyrics alloc] initWithTime:time andLyric:lrc];
        [_mArray addObject:l];
        NSLog(@"添加成功!");
        [self sortedByTime];
    }
    else
    {
        NSLog(@"时间重复，添加失败!");
    }
}

#pragma mark - 查询歌词
-(NSString *)searchLrc:(NSString *)time
{
    for (id obj in _mArray) {
        if ([[obj time] isEqualToString:time]) {
            NSLog(@"查询成功!");
            return obj;
        }
    }
    NSLog(@"查询失败!");
    return nil;
}

//-(void)play
//{
//    NSLog(@"正在播放....");
//
//    NSRange range = {4,2};
//    NSInteger wait_time = [[[[_mArray objectAtIndex:0] time] substringWithRange:range] intValue];
//    sleep((unsigned int)wait_time);
//    NSLog(@"%@",[_mArray objectAtIndex:0]);
//    for (NSUInteger i = 1; i < [_mArray count]; i++) {
//        //计算时间差
//        wait_time = [[[[_mArray objectAtIndex:i] time] substringWithRange:range] intValue] - [[[[_mArray objectAtIndex:i -1] time] substringWithRange:range] intValue];
//        if (wait_time < 0) {
//            wait_time = wait_time + 60;
//        }
//        sleep((unsigned int)wait_time);
//        NSLog(@"%@",[_mArray objectAtIndex:i]);
//    }
//}



@end
